# -*- coding: utf-8 -*-
"""userSetup.py by Erkan Ozgur Yilmaz (c) 2010

v1.0.0

Description
===========

The setup script for maya environment. Forces the user to set a valid project
and sequence from the interfacde

Changes
=======

1.0.0
-----

* New version number scheme.
* Removed MentalRay initialization.

v11.4.4
-------

 * Removed the unnecessary PATH environment key exports

 * Merged maya 2011 and 2009 versions of this script

v11.3.15
--------

 * moved some code outside of the skipUser check
 * added path variables for linux and osx

v11.3.12
--------

 * migrated userSetup.mel to this script completely

v10.12.29
---------

 * added auto plugin load for Mayatomr and objExport

v10.5.17
--------

 * modifications for Maya 2011 and Pymel 1.0.2

v10.2.6
-------

 * pm.isStandalone is not working under windows, switched to cmds.about(b=True)

v10.2.5
-------

 * Finally moved to python

"""

import sys
import os
import traceback

__version__ = "1.0.0"

# ----------------------------------------------------------------------------
# add environment variables relative to this path
here = ""
try:
    here = os.path.dirname(__file__)
except NameError, e:
    tb = traceback.extract_tb(sys.exc_info()[2])
    here = tb[0][0]

envpaths = [
    '../../../',
    '../../../oy-maya-scripts',
    '../../../oy-maya-scripts/mayaHacks',
    '../../../oy-maya-scripts/mayaHacks/2014',
    '../../../oy-maya-scripts/oyTools',
    '../../../oy-maya-scripts/plugins'
]

for path in envpaths:
    resolved_path = os.path.normpath(
        os.path.join(here, path)
    )

    print('appending : %s' % resolved_path)
    sys.path.append(resolved_path)

# now we can import pymel and others
import pymel
import pymel.core as pm
import maya.cmds as cmds

try:
    pm.Mel.source("HKLocalTools")
except pm.MelError:
    pass


def __pluginLoader(pluginName):
    if not pm.pluginInfo(pluginName, q=1, loaded=1):
        pm.loadPlugin(pluginName)


def __pluginUnloader(pluginName):
    if not pm.pluginInfo(pluginName, q=1, loaded=1):
        pm.unloadPlugin(pluginName)


__pluginUnloader('Mayatomr')
__pluginLoader('objExport')
__pluginLoader('closestPointOnCurve.py')
__pluginLoader('fbxmaya')
__pluginLoader('OpenEXRLoader')
__pluginLoader('tiffFloatReader')


# set the optionVar that enables hidden mentalray shaders
if pymel.versions.current() <= pymel.versions.v2012:
    pm.optionVar['MIP_SHD_EXPOSE'] = 1
    pm.runtime.SavePreferences()


# Change the default camera to Alexa
try:
    persp = pm.PyNode("persp")
    perspShape = persp.getShape()

    perspShape.horizontalFilmAperture.set(23.76/25.4)
    perspShape.verticalFilmAperture.set(13.365/25.4)
except pm.MayaNodeError:
    pass

# create environment variables for each Repository
from stalker import db
from anima.env import create_repo_vars
db.setup()
create_repo_vars()

# load shelves
import platform
from anima.env.mayaEnv import auxiliary

system_name = platform.system().lower()

shelves_path = '../../../../shelves'

if system_name == 'windows':
    shelf_names = ['kks_Tools']
elif system_name == 'linux':
    shelf_names = ['kks_Tools_linux']


for shelf_name in shelf_names:
    shelf_path = os.path.normpath(
        os.path.join(here, shelves_path, 'shelf_%s.mel' % shelf_name)
    ).replace('\\', '/')

    pm.evalDeferred('auxiliary.delete_shelf_tab("%s", confirm=False)' % shelf_name)
    pm.evalDeferred('auxiliary.load_shelf_tab("%s")' % shelf_path)

    print('shelf_path: %s' % shelf_path)
